<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "product".
 *
 * @property integer $productId
 * @property integer $categoryId
 * @property string $title
 * @property string $description
 * @property integer $number
 * @property string $price
 * @property string $cover
 * @property string $pictures
 * @property integer $issale
 * @property integer $ishot
 * @property integer $istui
 * @property string $saleprice
 * @property integer $ison
 * @property string $createTime
 *
 * @property Category $category
 */
class Product extends superModel {

    const AK = 'ePmKtzi8EmCOCGswcF0gYHt4NBYp7cAWWXR-2mfK';
    const SK = 'J6XK0LxuNWeXjLw2e7pTZjDUdxLOTxAiX5N44QHn';
    const DOMAIN = 'ofy6hewql.bkt.clouddn.com';
    const BUCKET = 'ghws';

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'product';
    }

    public static function getCTableName()
    {
        return '商品';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
                [ [ 'categoryId', 'title', 'number', 'price', 'cover', 'pictures', 'issale', 'ishot', 'istui', 'saleprice', 'ison' ], 'required' ],
                [ [ 'categoryId', 'number', 'issale', 'ishot', 'istui', 'ison' ], 'integer' ],
                [ [ 'description', 'pictures' ], 'string' ],
                [ [ 'price', 'saleprice' ], 'number' ],
                [ [ 'createTime' ], 'safe' ],
                [ [ 'title', 'cover' ], 'string', 'max' => 200 ],
                [ [ 'categoryId' ], 'exist', 'skipOnError' => true, 'targetClass' => Category::className(), 'targetAttribute' => [ 'categoryId' => 'categoryId' ] ],
        ];
    }

    public static $list = [ '是', '否' ];

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'productId'          => 'ID',
            'categoryId'  => '商品分类',
            'title'       => '商品名称',
            'description' => '商品描述',
            'number'      => '商品数量',
            'price'       => '商品价格',
            'cover'       => '商品封面',
            'pictures'    => '商品图片',
            'issale'      => '商品促销',
            'ishot'       => '商品热销',
            'istui'       => '商品推荐',
            'saleprice'   => '促销价格',
            'ison'        => '商品上架',
            'createTime'  => '创建时间',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCategory()
    {
        return $this->hasOne(Category::className(), [ 'categoryId' => 'categoryId' ])->inverseOf('products');
    }

    public function beforeSave($insert)
    {
//        $this->createTime = $this->getDateTime();
        if (parent::beforeSave($insert)) {
            if ($this->isNewRecord) {
                $this->createTime = $this->getDateTime();
            }
            return TRUE;
        }
        else {
            return FALSE;
        }
    }

}
